package com.ncmmall.mall.controller.business.order;

import com.ncmmall.application.business.cloudstore.CloudStoreApplication;
import com.ncmmall.domain.business.cloudstore.CloudStoreGoods;
import com.ncmmall.domain.business.cloudstore.QCloudStoreGoods;
import com.ncmmall.domain.business.goods.Goods;
import com.ncmmall.domain.business.goods.QGoods;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.primitives.Longs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Nullable;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author linxs
 * @Description: 云仓管理
 * @Date: 2020-04-29 23:45
 */
@Controller
@RequestMapping(value = "/merchant/cloudstore")
public class MerchantCloudStoreController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private CloudStoreApplication cloudStoreApplication;


    /**
     * 订单列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, PageParams pageParams, WebUser merchant) throws ParseException {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", Securitys.currentUser().getId());

        String hql = builderSQL("select distinct d.supplier from CloudStoreGoods d where d.merchant.id = :id and d.status = 1  ", params, merchant);
        QueryPage<WebUser> page = queryChannelService.findPage(hql, pageRequest.setParams(params), WebUser.class);

        page.setResult(Lists.transform(page.getResult(), new Function<WebUser, WebUser>() {
            @Nullable
            @Override
            public WebUser apply(@Nullable WebUser input) {

                double quantity = queryChannelService.selectSum("select ifnull(sum(d.quantity), 0) from mall_cloud_store_goods d where d.supplier_id = :mid and d.merchant_id = :id and d.status = 1 ", ImmutableMap.of("id", Securitys.currentUser().getId(), "mid", input.getId()));

                input.setGoodsNumber(new Double(quantity).intValue());

                return input;
            }
        }));

        model.addAttribute("page", page).addAttribute("params", merchant);

        return "merchant/cloudStore/list";
    }

    /**
     * 查看详情
     */
    @RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
    public String details(Model model, @PathVariable("id") long id, PageParams pageParams, CloudStoreGoods detail) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();
        params.put("mid", Securitys.currentUser().getId());
        params.put("id", id);

        String hql = builderOrderDetailSQL("select d from CloudStoreGoods d where  d.supplier.id = :id and d.merchant.id = :mid and d.status = 1 ", params, detail);
        QueryPage<CloudStoreGoods> page = queryChannelService.findPage(hql, pageRequest.setParams(params), CloudStoreGoods.class);

        detail.setId(id);
        model.addAttribute("page", page).addAttribute("params", detail);

        return "merchant/cloudStore/details";
    }

    /**
     * 选择要关联商品
     */
    @RequestMapping(value = "/chooseGoods", method = RequestMethod.GET)
    private String chooseGoods(Model model, PageParams pageParams, Goods coupon) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", Securitys.currentUser().getId());
        String hql = builderGoodsSQL("select u from Goods u where u.merchant.id = :id and u.status = 3 and u.marketingType = 0 ", params, coupon);
        QueryPage<Goods> page = queryChannelService.findPage(hql, pageRequest.setParams(params), Goods.class);

        model.addAttribute("page", page).addAttribute("params", coupon);

        return "merchant/cloudStore/chooseGoods";
    }

    /**
     * 一键关联
     */
    @RequestMapping(value = "/relevanceGoodses", method = RequestMethod.POST)
    @ResponseBody
    private Map relevanceGoodses(String ids, long id) {

        try {

            List<Goods> goodses = Lists.transform(Splitter.on("-").omitEmptyStrings().trimResults().splitToList(ids), new Function<String, Goods>() {
                @Nullable
                @Override
                public Goods apply(@Nullable String input) {
                    return queryChannelService.findOne(QGoods.goods.id.eq(Longs.tryParse(input)), Goods.class);
                }
            });

            CloudStoreGoods cloudStoreGoods = queryChannelService.findOne(QCloudStoreGoods.cloudStoreGoods.id.eq(id), CloudStoreGoods.class);

            cloudStoreApplication.relevance(cloudStoreGoods, goodses);


            return ImmutableMap.of("status", true);

        } catch (Exception e) {

            return ImmutableMap.of("status", false);
        }

    }


    private String builderGoodsSQL(String sql, Map<String, Object> params, Goods goods) {
        return sql;
    }


    private String builderOrderDetailSQL(String sql, Map<String, Object> params, CloudStoreGoods detail) {
        return sql;
    }

    private String builderSQL(String sql, Map<String, Object> params, WebUser merchant) {
        return sql;
    }

}
